package org.scrm.service.impl;

import com.google.common.base.Function;
import org.scrm.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;



/**
 * @author xu yuanjun
 * @version 1.0.0
 * @createTime 2022/03/04 15:07:00
 * @ClassName QueryGrantTypeService
 * @Description TODO
 */
@Service
public class QueryGrantTypeServiceImpl {

    @Autowired
    private OrderService orderService;

    private Map<String, Function<String,String>> grantTypeMap=new HashMap<>();



    /**
     *  初始化业务分派逻辑,代替了if-else部分
     *  key: 优惠券类型
     *  value: lambda表达式,最终会获得该优惠券的发放方式
     */
    @PostConstruct
    public void dispatcherInit(){
        grantTypeMap.put("红包",resourceId->orderService.redPaper(resourceId));
        grantTypeMap.put("购物券",resourceId->orderService.shopping(resourceId));
        grantTypeMap.put("qq会员",resourceId->orderService.QQVip(resourceId));
    }

    public String getResult(String resourceType){
        //Controller根据 优惠券类型resourceType、编码resourceId 去查询 发放方式grantType
        Function<String,String> result= grantTypeMap.get(resourceType);
        if(result!=null){
            //传入resourceId 执行这段表达式获得String型的grantType
            return result.apply(resourceType);
        }
        return "查询不到该优惠券的发放方式";
    }



}
